package ua.org.nuos.sdms.clientgui.client.context;

import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import ua.org.nuos.sdms.clientgui.client.components.window.CustomDialog;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.InfoMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.ShowDialogMessageEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.ShowDialogMessageListener;
import ua.org.nuos.sdms.clientgui.client.listeners.ShowErrorMessageListener;
import ua.org.nuos.sdms.clientgui.client.listeners.ShowInfoMessageListener;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 19.02.12
 * Time: 16:39
 * To change this template use File | Settings | File Templates.
 */
public class NotificationManager implements ShowErrorMessageListener, ShowInfoMessageListener, ShowDialogMessageListener {

    private static final String ERROR_TITLE = "Ошибка.";

    private Window window;

    public NotificationManager(Window window) {
        this.window = window;
    }

    @Override
    public void showMessage(ErrorMessageEvent event) {
        window.showNotification(ERROR_TITLE, event.getMessage(), Window.Notification.TYPE_ERROR_MESSAGE);
    }

    @Override
    public void showMessage(InfoMessageEvent event) {
        String caption = event.getCaption();
        if (caption != null) {
            window.showNotification(caption, event.getMessage(), Window.Notification.TYPE_TRAY_NOTIFICATION);
        } else {
            window.showNotification(event.getMessage(), Window.Notification.TYPE_TRAY_NOTIFICATION);
        }
    }

    @Override
    public void showDialog(final ShowDialogMessageEvent event) {
        CustomDialog.Listener listener = new CustomDialog.Listener() {
            @Override
            public void onClose(CustomDialog.Event e) {
                event.getResultHandler().dialogResultProcess(e.isConfirm());
            }
        };
        CustomDialog dialog = new CustomDialog(listener, event.getCaption(), event.getMessage(), Label.CONTENT_XHTML, "Да", "Нет");
        dialog.show();
    }
}
